<?php
class Admins extends MY_Controller
{
	public function __construct(){
		parent::__construct();
		$this->load->helper('url');
		$this->load->model('sysadmin/admins_model');
	}
	
	/**
	 * 管理首页
	 * Enter description here ...
	 */
	public function index($page = 0){
		
		$data['title'] = '管理员';
		$per_page = 20;
		
		$admins = $this->db->get('admin');
		
//		print_r($admins);
		$data['list_current'] = array('admins','admins_list');
		$data['js'] = array('admins');
		
		
		$this->load->library ( 'pagination' );		
		$config ['base_url'] = '/sysadmin/admins/index';
		$config ['total_rows'] = $admins->num_rows();
		$config ['per_page'] = $per_page;
		$config ['uri_segment'] = 4;
		$this->pagination->initialize ( $config );
		
		$admins = $this->admins_model->page($page, $per_page);
		$data['admins'] = $admins;
		
		$data['pageinfo'] =  $this->pagination->create_links ();		
		$this->load->view('admin/header', $data);
		$this->load->view('admin/admins', $data);
		$this->load->view('admin/footer');
	}
	
	public function add($id = NULL){
		$data['title'] = '添加管理员';
		$data['list_current'] = array('admins','admins_add');
		$data['js'] = array('admin_add');
		if(!$this->input->post('username')){
			if(isset($id)){
				$id = intval($id);
				if($id !=0){
					$admin = $this->db->get_where('admin', array('id'=>$id));
					$data['admin'] = $admin;
				}
			}
			
			$this->load->view('admin/header', $data);
			$this->load->view('admin/admins_add', $data);
			$this->load->view('admin/footer');
		}else{
			$now = date("Y-m-d H:i:s");
			$setarr = array('createtime'=>$now, 'updatetime'=>$now);
			//检查用户名
			$u = $this->input->post('username');
			if($u == ''){
				die('-1');
			}
			if($this->input->post('password')=='' || ($this->input->post('password') != $this->input->post('password1'))){
				die('-3');//密码输入有问题
			}
			$setarr['username'] = $u;
			$setarr['password'] = $this->input->post('password');
			if(!$this->input->post('id')){
				$exist = $this->db->get_where('admin', array('username'=>$u));
				if($exist->result()){
					die('-2');//用户名存在
				}
				
				$this->db->insert('admin', $setarr);
				$insert_id = $this->db->insert_id();
				echo $insert_id;die();
			}else{
				//编辑
				$this->db->where('id', $this->input->post('id'));
				if($this->db->update('admin', $setarr)){
					echo 1;
				}else{
					echo -1;die();
				}
				
			}
		}
	}
	
	public function delete($id = NULL){
		if($id){
			$id = explode('-', $id);
			$this->db->where_in('id', $id);
			if($this->db->delete('admin')){
				echo 1;
			}else{
				echo -1;
			}
			
		}
	}
	
	public function get(){
		$username = $this->input->post('username');
		if($username != NULL){
			$this->db->select('username,id');
			$d = $this->db->get_where('admin',array('username'=>$username));
			echo json_encode($d->result());			
		}
	}
	public function getbyid(){
		$id = $this->input->post('id');
		if($id != NULL){			
			$d = $this->db->get_where('admin',array('id'=>$id));
			echo json_encode($d->result());			
		}
	}
	
	public function logout(){
		$this->session->sess_destroy();
		redirect(site_url('/sysadmin/login'));
	}

	/**
	* 登录历史日志
	*/
	public function loginlog($page = 0){
		$data['title'] = '管理员登录日志';
		$per_page = 10;
		
		
		$n = $this->db->get('adminloginlog');
	
		$data['list_current'] = array('admins','admins_loginlog');
		$data['js'] = array();
		
		$this->load->library ( 'pagination' );		
		$config ['base_url'] = site_url('/sysadmin/admins/loginlog');
		$config ['total_rows'] = $n->num_rows();
		$config ['per_page'] = $per_page;
		$config ['uri_segment'] = 4;
		$this->pagination->initialize ( $config );
		
		$sql = "SELECT * FROM {$this->db->dbprefix}adminloginlog
ORDER BY id DESC
LIMIT {$page},{$per_page}";

		$o = $this->db->query($sql);
		$values = $o->result();
		$data['values'] = $values;
		
		$data['pageinfo'] =  $this->pagination->create_links ();
		
		$this->load->view('admin/header', $data);
		$this->load->view('admin/adminloginlog', $data);
		$this->load->view('admin/footer');
	}
}
// END Admins Class

/* End of file admins.php */
/* Location: ./application/controller/sysadmin/Admins.php */
